package team.seekers.hula.user.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.hula.common.annotation.ApiResponseBody;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.common.entity.result.ApiException;
import team.seekers.hula.common.utils.ParamCheckUtils;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.user.user.entity.dto.UserPageQueryDTO;
import team.seekers.hula.user.user.entity.dto.UserUpdateDTO;
import team.seekers.hula.user.user.entity.vo.UserPageListVO;
import team.seekers.hula.user.user.service.UserService;

import static team.seekers.hula.common.entity.result.ApiExceptionEnum.USER_ACCOUNT_ALREADY_EXIST;

/**
 * @author Godc
 * 这里是管理员对用户的一些操作
 */
@Controller
@ApiResponseBody
@RequestMapping("/admin/user")
public class UserManageController {
    @Autowired
    private UserService userService;

    //封禁用户
    @GetMapping("/ban")
    public void banUser(@RequestParam("userId") String userId){

        if(ParamCheckUtils.isNotRightId(userId)){
            //return R.fail(ResultCode.MAIN_GET_FAIL);
        }
        userService.ban(userId);
    }

    @GetMapping("/unban")
    public void unbanUser(@RequestParam("userId") String userId) {
        userService.unban(userId);
    }

    //将用户设为大V
    @GetMapping("/setV")
    public void setUserToV(@RequestParam("userId") String userId){
        if(ParamCheckUtils.isNotRightId(userId)){
            //return R.fail(ResultCode.MAIN_GET_FAIL);
        }
        userService.setV(userId);
    }

    // page
    @GetMapping("/page")
    public PageResult<UserPageListVO> list(UserPageQueryDTO userQuery) {
        return userService.page(userQuery);
    }

    @PostMapping("/update")
    public void update(@RequestBody UserUpdateDTO userUpdateDTO) {
        if(StringUtils.isNotBlank(userUpdateDTO.getAccount())){
            //校验用户名
            QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
            wrapper.eq("account", userUpdateDTO.getAccount());
            int count = userService.count(wrapper);
            if (count != 0){
                throw new ApiException(USER_ACCOUNT_ALREADY_EXIST);
            }
        }
        userService.update(userUpdateDTO);
    }
}
